package com.night.attendance.serviceImpl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.night.attendance.feign.CourseManagerFeign;
import com.night.attendance.mapper.AttendanceConfigMapper;
import com.night.attendance.mapper.StudentAttendanceMapper;
import com.night.attendance.service.AttendanceConfigService;
import com.night.common.entity.PageBean;
import com.night.common.entity.ResultVo;
import com.night.common.entity.attendance.AttendanceConfigDO;
import com.night.common.entity.attendance.AttendanceConfigQueryDO;
import com.night.common.entity.attendance.AttendanceMessageDO;
import com.night.common.entity.attendance.StudentAttendanceDO;
import com.night.common.entity.student.Student;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author Dark-Morning
 * 2022/10/18 17:28
 */
@Service
@SuppressWarnings("all")
public class AttendanceConfigServiceImpl implements AttendanceConfigService {
    @Autowired
    AttendanceConfigMapper attendanceConfigMapper;

    @Autowired
    StudentAttendanceMapper studentAttendanceMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    CourseManagerFeign courseManagerFeign;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void saveOne(AttendanceConfigDO attendanceConfigDO) {
        ResultVo<List<Student>> resultVo = courseManagerFeign.getAllStudentId(attendanceConfigDO.getCourseManagerId());
        if (resultVo.getCode() == 200) {
            List<Student> students = resultVo.getData();
            System.out.println(students.getClass());
            System.out.println(students.get(0).getClass());
            List<StudentAttendanceDO> list = new ArrayList<>();
            GeoOperations geoOperations = redisTemplate.opsForGeo();
            String[] index = attendanceConfigDO.getSignPosition().split("-");
            double x = Double.parseDouble(index[0]);
            double y = Double.parseDouble(index[1]);
            String attendanceUUID = UUID.randomUUID().toString().replace("-", "") + attendanceConfigDO.getCourseNumber();
            geoOperations.add(attendanceUUID, new Point(x, y), "o");
            redisTemplate.expire(attendanceUUID, attendanceConfigDO.getSignIndate(), TimeUnit.MINUTES);
            attendanceConfigDO.setCourseStartTime(DateUtil.now());
            attendanceConfigMapper.insertOne(attendanceConfigDO);
            rabbitTemplate.setExchange("smart-school-attendance");
            rabbitTemplate.setRoutingKey("attendance");
            MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration(attendanceConfigDO.getSignIndate() * 60 * 1000 + "");
                    return message;
                }
            };
            for (Student student : students) {
                StudentAttendanceDO studentAttendanceDO = new StudentAttendanceDO(
                        attendanceConfigDO.getCourseManagerId(),
                        attendanceConfigDO.getCourse(),
                        "",
                        "0",
                        null,
                        student.getId(),
                        student.getStudentNumber(),
                        student.getName(),
                        "",
                        "",
                        attendanceConfigDO.getId(),
                        attendanceUUID,
                        attendanceConfigDO.getSignArea()
                );
                list.add(studentAttendanceDO);
                rabbitTemplate.convertAndSend(new AttendanceMessageDO(student.getId(),attendanceConfigDO.getId()),messagePostProcessor);
            }
            studentAttendanceMapper.insertBatch(list);


        }
    }

    @Override
    public void listAttendanceConfig(AttendanceConfigQueryDO attendanceConfigQueryDO) {
        PageBean<List<AttendanceConfigDO>> pageBean = attendanceConfigQueryDO.getPageBean();
        Page<AttendanceConfigDO> p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSize());
        List<AttendanceConfigDO> data = attendanceConfigMapper.selectByTeacherId(attendanceConfigQueryDO);
        pageBean.setData(data);
        pageBean.setTotalNum((int) p.getTotal());
        pageBean.setTotalPages(p.getPages());
    }

}
